package com.kpijavatraining6.module2.sessionTools;

import com.kpijavatraining6.module2.mainMenu.GameLogic;
import com.kpijavatraining6.module2.sessionTools.AuthorisationMenu;

import java.io.*;
import java.util.Scanner;

public class UserStatistics {
    private String userName;
    private int totalScore;
    private int totalMaxScore;
    private int userScore;
    private int userMaxScore;
	public UserStatistics(String name, int score,int maxScore) {
        userName = name;
        userScore = score;
        userMaxScore = maxScore;
    }
    public  UserStatistics(String name) {
        userName = name;
        userScore = 0;
        userMaxScore = 0;
    }
	public void saveStatistic(){
        loadOldStatistic();
        try {
    		FileWriter fileWritter = new FileWriter(createUserFilePath());
    	    BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
            int newTotalScore = userScore + totalScore;
            int newTotalMaxScore = userMaxScore + totalMaxScore;
    	    bufferWritter.write(newTotalScore+" "+newTotalMaxScore);
    	    bufferWritter.close();
        } catch(IOException e){
	    	e.printStackTrace();
	    }
    }
    private void loadOldStatistic() {
        File file = new File(createUserFilePath());
        FileInputStream fin;
        try {
            fin = new FileInputStream(file);
            Scanner scanner = new Scanner(fin);
            totalScore = scanner.nextInt();
            totalMaxScore = scanner.nextInt();
        } catch (FileNotFoundException e) {
            totalScore = 0;
            totalMaxScore = 0;
        }
    }
    public void showStatistic() {
        loadOldStatistic();
        System.out.printf("%s:  %d from %d\n",userName, totalScore,totalMaxScore);
    }

	private String createUserFilePath() {
		String filePath = userName + "_statistics.txt";
		return filePath;
	}

}

